﻿using Blog.Core.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Rf.Common;
using Rf.Common.HttpContextUser;
using Rf.Repository.Model;
using Rf.Support.Controllers;
using Rf.Support.IServices;
using Rf.Support.Model;
using Rf.Support.Model.Models;
using SqlSugar;
using System.Linq.Expressions;

namespace Rf.Support.Api.Controllers
{
    /// <summary>
    /// 数据字典
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class DictController : BaseApiController
    {
        private readonly IDictServices _dictServices;

        public DictController(IDictServices dictServices)
        {
            _dictServices = dictServices;
        }
        /// <summary>
        /// 分页获取
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="pid">父级</param>
        /// <param name="page">页码</param>
        /// <param name="intPageSize">页容量</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<Dict>>> Get(string name = "", long pid = 0, int page = 1, int intPageSize = 50)
        {
            Expression<Func<Dict, bool>> whereExpression = Expressionable.Create<Dict>()
               .AndIF(!string.IsNullOrEmpty(name), c => c.DictLabel.Contains(name))
               .And(c => c.ParentId == pid)
               .ToExpression();
            return new MessageModel<PageModel<Dict>>()
            {
                msg = "获取成功",
                success = true,
                response = await _dictServices.QueryPage(whereExpression, page, intPageSize)
            };
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<MessageModel<Dict>> Get(string id)
        {
            var data = new MessageModel<string>() { success = true };
            var model = await _dictServices.QueryById(id);
            return Success(model);
        }


        /// <summary>
        /// 添加字典
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        // POST: api/User
        [HttpPost]
        public async Task<MessageModel<string>> Post([FromBody] Dict dict)
        {

            var id = (await _dictServices.Add(dict));
            return id > 0 ? Success(id.ObjToString(), "添加成功") : Failed("添加失败");

        }

        /// <summary>
        /// 更新字典
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        // PUT: api/User/5
        [HttpPut]
        public async Task<MessageModel<string>> Put([FromBody] Dict dict)
        {
            if (dict == null || dict.Id <= 0)
                return Failed("缺少参数");
            return await _dictServices.Update(dict) ? Success(dict?.Id.ObjToString(), "更新成功") : Failed("更新失败");
        }


        /// <summary>
        /// 删除字典
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        // DELETE: api/ApiWithActions/5
        [HttpDelete]
        public async Task<MessageModel<string>> Delete(long id)
        {
            var data = new MessageModel<string>();
            if (id <= 0) return Failed();
            var userDetail = await _dictServices.QueryById(id);
            if (userDetail == null) return Success<string>(null, "字典不存在");
            userDetail.IsDeleted = true;
            return await _dictServices.Update(userDetail) ? Success(userDetail?.Id.ObjToString(), "删除成功") : Failed();
        }
    }
}
